var {
  getRandomlist
} = require("../../api/exam")

// pages/exam/exam.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    pageSize: 50,
    startclientY: '',
    presentindex: 0,
    datalist: [],
    time: 20 * 60,
    timeText: "",
    success: 0,
    classStatus: ""
  },

  onClose() {
    this.setData({
      show: false
    })
  },
  goUp() {
    this.setData({
      show: true
    })
  },

  //// 滑出

  start(e) {
    this.setData({
      startclientY: e.touches[0].clientY //起始点的clientY
    })
  },

  showFun(e) {
    if (e.touches[0].clientY > this.data.startclientY) {
      this.setData({
        show: false
      })
      console.log(1)
    } else {
      this.setData({
        show: true
      })
    }
  },

  hideFun(e) {
    console.log(e, 'end')
  },
  ///// 下一题
  next(e) {
    let data = this.data.datalist
    if (JSON.parse(this.data.datalist[this.data.presentindex].answers).join(",") == e.detail.params.join(",")) {
      data[e.detail.query].classStatus = 'success'
    } else {
      data[e.detail.query].classStatus = 'error'
    }
    data[e.detail.query].respondence = e.detail.params
    this.setData({
      presentindex: this.data.presentindex + 1,
      datalist: data
    })
    console.log(this.data.datalist[this.data.presentindex-1])
  },
  //// 上一题
  uptopic(e) {
    let data = this.data.datalist
    data[this.data.presentindex].respondence = e.detail.sort()
    this.setData({
      presentindex: this.data.presentindex - 1,
      datalist: data
    })
  },
  ///// 加分
  successAdd(e) {
    if (JSON.parse(this.data.datalist[e.detail].answers).toString() == this.data.datalist[e.detail].respondence.toString()) {
      this.setData({
        success: this.data.success + 1,
        classStatus: 2
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let {
      data
    } = await getRandomlist()
    this.setData({
      datalist: data
    })
    // 时间百分比
    this.setData({
      time: this.data.time / (20 * 60).toFixed(2) * 100,
    })
    
    let timeStatus=  setInterval(() => {
      this.setData({
        time: this.data.time - 1,
        timeText:this.timeTextFun()
      })
      if(this.data.time <0){
        clearInterval(timeStatus)
      }
    }, 1000)
  },

  ///// 时间文本
  timeTextFun() {
    return (
      (Math.floor(this.data.time / 60) < 10 ?
        "0" + Math.floor(this.data.time / 60) :
        Math.floor(this.data.time / 60)) +
      " : " +
      (this.data.time % 60 < 10 ? "0" + (this.data.time % 60) : this.data.time % 60)
    );
  },

  //// 题号选择吧
  questionnumber(e){
    console.log(e.target);
    this.setData({
      presentindex : e.target.dataset.questionindex
    })
    console.log(this.data.presentindex)
    // console.log(1)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})